import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/subject',
    children: []
  },

  {
    path: '/chart',
    component: Layout,
    children: [
      {
        path: '',
        name: 'chart',
        component: () => import('@/views/chart/chart.vue'),
        meta: {
          icon: 'el-icon-pie-chart',
          title: '数据概览',
          roles: ['超级管理员', '管理员', '老师']
        }
      }
    ]
  },
  {
    path: '/user',
    component: Layout,
    children: [
      {
        path: '',
        name: 'user',
        component: () => import('@/views/user/user.vue'),
        meta: {
          icon: 'el-icon-user',
          title: '用户列表'
        }
      }
    ]
  },
  {
    path: '/question',
    component: Layout,
    children: [
      {
        path: '',
        name: 'question',
        component: () => import('@/views/question/question.vue'),
        meta: {
          icon: 'el-icon-edit-outline',
          title: '题库列表'
        }
      }
    ]
  },
  {
    path: '/business',
    component: Layout,
    children: [
      {
        path: '',
        name: 'business',
        component: () => import('@/views/business/business.vue'),
        meta: {
          icon: 'el-icon-office-building',
          title: '企业列表'
        }
      }
    ]
  },
  {
    path: '/subject',
    component: Layout,
    children: [
      {
        path: '',
        name: 'subject',
        component: () => import('@/views/subject/subject.vue'),
        meta: {
          icon: 'el-icon-notebook-2',
          title: '学科列表'
        }
      }
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  })

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
